var memberUtil=require('../../utils/memberUtil');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    popup: 1,
    userDataInfo:{},
    files: [],
    nickName:"",
    baseImageUrl:app.globalData.baseUrl,
    mobile:"",
    nickName:"",
    tximg:"",
  },
  bindImgTab: function (e) {
    this.setData({
      popup: 0
    })
  },
  bindcolTab: function (e) {
    this.setData({
      popup: 1
    })
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count:1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFiles[0];
        if(tempFilePaths.size>2*1024*1024){
            wx.showToast({
                title: '图片大小不能超过2M',
                icon: 'none',
                duration: 2000
            })
          return false;
        }
        that.data.userDataInfo["headImageUrl"]=tempFilePaths.path;
        that.setData({
          userDataInfo: that.data.userDataInfo
        });

        wx.uploadFile({
          url: app.globalData.baseUrl+'/member/headImageUpload', //仅为示例，非真实的接口地址
          filePath: tempFilePaths.path,
          name: 'headImage',
          formData:{
            'memberId': that.data.userInfo.id
          },
          success: function(res){
            var data = res.data;
            that.data.userDataInfo["headImageUrl"]= data;
            that.setData({
              userDataInfo: that.data.userDataInfo
            });

          },
            fail:function (res) {
                console.log(JSON.stringify(res))
            }
        })

      }
    })
  },
  xgFu:function(){
    var that=this;
    var memberInfoJson=that.data.userDataInfo;
    memberInfoJson["username"]=that.data.nickName;
    memberInfoJson["phoneNumber"]=that.data.mobile;
    if(memberInfoJson.username=="" || memberInfoJson.username==null){
      wx.showToast({
        title: '用户昵称不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(memberInfoJson.headImageUrl=="" || memberInfoJson.headImageUrl==null){
      wx.showToast({
        title: '用户头像不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if(that.data.mobile=="" || that.data.mobile==null){
      wx.showToast({
        title: '用户手机号不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.request({
      url: app.globalData.baseUrl+'/member/updateMemberBaseInfo',
      header: app.globalData.headerPost,
      method: 'POST',
      data:JSON.stringify(memberInfoJson),
      success:function(res) {
        if (Number(res.data.status) == 200) {
          wx.showToast({
            title: '个人资料修改成功',
            icon: 'none',
            duration: 2000
          })
          wx.switchTab({
            url: '/pages/tabBar/my/user'
          });
        } else {
          wx.showToast({
            title: '个人资料修改失败',
            icon: 'none',
            duration: 2000
          })
          return false;
        }
      }
    })
  },
  focus:function(e){
    var that=this;
    var inputV= e.detail.value;
    that.setData({
      nickName:inputV
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.setData({
      userInfo:wx.getStorageSync("loginUserInfo").userInfo==undefined?"":wx.getStorageSync("loginUserInfo").userInfo,
    })
    if(that.data.userInfo.id==undefined) {
      memberUtil.getLogin(app,0,1,that,2);
    }
    var mobile=options.mobile,nickName=options.nickName;
    if(mobile!=null && mobile!="" && mobile!=undefined){
      this.setData({
        mobile:mobile
      })
    }

    if(nickName!=null && nickName!="" && nickName!=undefined){
      this.setData({
        nickName:nickName
      })
    }
  },
  changeMobile:function(e){
    var that=this;
    var mobile= e.currentTarget.dataset.mobile;
    if(mobile==null || mobile=="" || mobile==undefined){
      mobile="";
    }
    wx.navigateTo({
      url: '/pages/my/bindmobile?mobile='+mobile+"&nickName="+that.data.nickName
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    var memberIdJson={memberId:this.data.userInfo.id}
    wx.request({
      url: app.globalData.baseUrl+'/member/findMemberBaseInfoByMemberId',
      header: app.globalData.headerPost,
      method: 'POST',
      data:JSON.stringify(memberIdJson),
      success:function(res) {
        if (Number(res.statusCode) == 200) {
          var mobile="";
          if(res.data.phoneNumber!=null || res.data.phoneNumber!=undefined){
            mobile=res.data.phoneNumber;
          }
          that.setData({
            mobile:mobile,
            userDataInfo: res.data,
            nickName:that.data.nickName==""?res.data.username:that.data.nickName
          })
        } else {
          that.setData({
            userDataInfo: {}
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that=this;
    return {
      title: '我的二维码',
      path: "/pages/shop/shareSuccess?parentId=" + that.data.userInfo.id + "&enterType=0",
      imageUrl: app.globalData.loginUserInfo.erweimaUrl
    }
  }
})